package com.zhiying.web.controller;

import com.zhiying.shiro.ContentType;
import com.zhiying.shiro.RequestPermission;
import com.zhiying.web.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping("index")
    public String toEnterShopIndex(HttpServletRequest request){
        String context = request.getContextPath();
        System.out.println("来自admin controller: "+context);
        return "login";
    }

    @RequestPermission(roles={"Admin"},resource = {"button:login:handler"},isSecurity = true,returnType=ContentType.textjson)
    @RequestMapping(value = "/autoLogin",method = RequestMethod.GET)
    @ResponseBody
    public String autoLogin(HttpServletRequest request, HttpServletResponse response) {
        return loginService.autoLogin(request, response);
    }

    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login2( HttpServletRequest request, HttpServletResponse response){
        return loginService.loginByNameAndPsw(request,response);
    }

}
